<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="909px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="活动标题" prop="title">
            <el-input v-model="dataForm.title" placeholder="活动标题"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原价" prop="originalPrice">
            <el-input v-model="dataForm.originalPrice" placeholder="原价"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-col :span="12">
        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model="dataForm.originalPrice" placeholder="原价"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="抢购价" prop="buyingPrice">
          <el-input v-model="dataForm.buyingPrice" placeholder="抢购价"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="门店名称" prop="storeId">
          <el-select v-model="dataForm.storeId" placeholder="请选择门店名称" style="width:100%">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="项目名称" prop="itemsId">
          <el-select v-model="dataForm.itemsId" placeholder="请选择项目名称" style="width:100%">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="活动开始日期" prop="startDate">
          <el-date-picker v-model="dataForm.startDate" type="datetime" placeholder="选择日期时间" default-time="12:00:00" style="width:100%"></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="活动结束日期" prop="endDate">
          <el-date-picker v-model="dataForm.endDate" type="datetime" placeholder="选择日期时间" default-time="12:00:00" style="width:100%"></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="活动描述" prop="introduction">
          <el-input type="textarea" :rows="2" placeholder="请输入活动描述" v-model="dataForm.introduction"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="活动规则" prop="rules">
          <el-input type="textarea" :rows="2" placeholder="请输入活动规则" v-model="dataForm.rules"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="详情" prop="details">
          <el-input type="textarea" :rows="2" placeholder="请输入商品详情" v-model="dataForm.details"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="轮播图" prop="banner">
          <el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="dataForm.fileList" list-type="picture-card">
            <el-button size="small" type="text">轮播图上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          originalPrice: '',
          buyingPrice: '',
          banner: '',
          startDate: '',
          endDate: '',
          introduction: '',
          rules: '',
          details: '',
          status: '',
          storeId: '',
          itemsId: '',
          fileList: [{ name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }],
          createTime: ''
        },
        options: [],
        dataRule: {
          title: [
            { required: true, message: '活动标题不能为空', trigger: 'blur' }
          ],
          originalPrice: [
            { required: true, message: '原价不能为空', trigger: 'blur' }
          ],
          buyingPrice: [
            { required: true, message: '抢购价不能为空', trigger: 'blur' }
          ],
          banner: [
            { required: true, message: '轮播图不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '活动开始日期不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '活动结束日期不能为空', trigger: 'blur' }
          ],
          introduction: [
            { required: true, message: '活动描述不能为空', trigger: 'blur' }
          ],
          rules: [
            { required: true, message: '活动规则不能为空', trigger: 'blur' }
          ],
          details: [
            { required: true, message: '详情不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0禁用 1正常不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          itemsId: [
            { required: true, message: '项目名称不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/activity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.activity.title
                this.dataForm.originalPrice = data.activity.originalPrice
                this.dataForm.buyingPrice = data.activity.buyingPrice
                this.dataForm.banner = data.activity.banner
                this.dataForm.startDate = data.activity.startDate
                this.dataForm.endDate = data.activity.endDate
                this.dataForm.introduction = data.activity.introduction
                this.dataForm.rules = data.activity.rules
                this.dataForm.details = data.activity.details
                this.dataForm.status = data.activity.status
                this.dataForm.storeId = data.activity.storeId
                this.dataForm.itemsId = data.activity.itemsId
                this.dataForm.createTime = data.activity.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/activity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'originalPrice': this.dataForm.originalPrice,
                'buyingPrice': this.dataForm.buyingPrice,
                'banner': this.dataForm.banner,
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'introduction': this.dataForm.introduction,
                'rules': this.dataForm.rules,
                'details': this.dataForm.details,
                'status': this.dataForm.status,
                'storeId': this.dataForm.storeId,
                'itemsId': this.dataForm.itemsId,
                'createTime': this.dataForm.createTime
              })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleRemove(file, fileList) {
        console.log(file, fileList)
      },
      handlePreview(file) {
        console.log(file)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .upload-demo {
    .el-upload--picture-card {
      background-color: #fbfdff;
      border: 1px dashed #c0ccda;
      border-radius: 6px;
      box-sizing: border-box;
      width: 60px;
      height: 60px;
      display: inline-flex;
      align-items: center;
      line-height: normal;
    }

    .el-upload-list--picture-card {
      .el-upload-list__item {
        width: 60px;
        height: 60px;
      }
    }
  }
</style>
